/**
 * 
 */
package edu.sjtu.seda.experiment;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author john.zhu
 *
 */
public class ThreadBased {
	private ExecutorService threadpool;
	private int threadpoolSize;
	
	public ThreadBased(int size){
		this.threadpoolSize = size;
		init();
	}
	
	private void init(){
		threadpool = Executors.newFixedThreadPool(threadpoolSize);
	}
	
	public void execute(Runnable run){
		threadpool.execute(run);
	}
	
	public boolean isShutdown(){
		return threadpool.isTerminated();
	}
	
	public void destroy(){
		threadpool.shutdown();
		try {
			threadpool.awaitTermination(2, TimeUnit.MINUTES);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
